<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm" label-width="100px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="管理单位：" prop="districtId">
                            <el-select v-model="listQuery.districtId" clearable @change="handleQuchange">
                                <el-option v-for="item in areaList" :label="item.dname" :value="item.did"
                                    :key="item.did"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="4">
                        <el-form-item label="管理部：" prop="houseAdminId">
                            <el-select v-model="listQuery.houseAdminId" clearable>
                                <el-option v-for="item in houseAdminList" :label="item.aname" :value="item.aid"
                                    :key="item.aid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="管片代码：" prop="blockGroupId">
                            <el-input v-model="listQuery.blockGroupId"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="所编号：" prop="blockId">
                            <el-input v-model="listQuery.blockId"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="所地址：" prop="blockAddress">
                            <el-input v-model="listQuery.blockAddress"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="4">
                        <el-form-item label="承租人：" prop="renter">
                            <el-input v-model="listQuery.renter"></el-input>
                        </el-form-item>
                    </el-col>

                    <!-- <el-col :span="4">
                        <el-form-item label="报修编号：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col> -->


                    <el-col :span="4">
                        <el-form-item label="报修日期：" prop="repairDate">
                            <el-date-picker v-model="listQuery.repairDate" type="date" placeholder="年/月/日">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="是否转工单：" prop="isGongdan">
                            <el-select v-model="listQuery.isGongdan" clearable>
                                <el-option label="是" :value="1" :key="1"></el-option>
                                <el-option label="否" :value="0" :key="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <div style="padding-left: 90px;">
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">工单录入</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">

                <el-table-column align="center" label="管理部（分中心）" prop="districtName" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="管片" prop="blockGroupName">
                </el-table-column>
                <el-table-column align="center" label="所编号" prop="blockId">
                </el-table-column>
                <el-table-column align="center" label="所地址/维修地址" prop="blockAddress">
                </el-table-column>
                <el-table-column align="center" label="报修日期" prop="repairDate">
                    <template slot-scope="scope">
                        {{getTime(scope.row.repairDate)}}
                    </template>
                </el-table-column>
                <el-table-column align="center" label="报修部位" prop="repairLocation">
                </el-table-column>
                <el-table-column align="center" label="是否约修" prop="isBook">
                    <template slot-scope="scope">
                        {{scope.row.isBook == 1 ? '是' : '否'}}
                    </template>
                </el-table-column>

                <el-table-column align="center" label="查勘后报修日期" prop="repairDateAfterSurvey">
                    <template slot-scope="scope">
                        {{getTime(scope.row.repairDateAfterSurvey)}}
                    </template>
                </el-table-column>
                <el-table-column align="center" label="报修项目" prop="repairItems">
                </el-table-column>
                <el-table-column align="center" label="问题说明" prop="problemDescription">
                </el-table-column>
                <el-table-column align="center" label="接待人员" prop="receptionist">
                </el-table-column>
                <el-table-column align="center" label="说明" prop="instructions">
                </el-table-column>

                <el-table-column align="center" label="操作" width="200" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleBantuo(scope.row)">接修转工单</el-button>

                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '报修录入' : isAdd == 'update' ? '报修修改' : '报修详情'" :visible.sync="dialogVisible"
            width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="150px"
                size="mini">

                <el-form-item label="区房屋管理单位：" prop="districtId">
                    <el-select v-model="formData.districtId" @change="handleFormQuchange">
                        <el-option v-for="item in areaList" :label="item.dname" :value="item.did"
                            :key="item.did"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="管理部(分中心)：" prop="houseAdminId">
                    <el-select v-model="formData.houseAdminId" @change="handleHouseChange">
                        <el-option v-for="item in houseFormAdminList" :label="item.aname" :value="item.aid"
                            :key="item.aid"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="管片代码：" prop="blockGroupId">
                    <el-select v-model="formData.blockGroupId" @change="handleGuanpianChange">
                        <el-option v-for="item in guanpianList" :label="item.groupnew" :value="item.groupnumber"
                            :key="item.groupnumber"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="所号：" prop="blockId">
                    <el-select v-model="formData.blockId" @change="handleSuoChange">
                        <el-option v-for="item in suoList" :label="item.creatingdeptno" :value="item.blockid"
                            :key="item.blockid"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="所地址：" prop="blockAddress">
                    <el-input v-model="formData.blockAddress"></el-input>
                </el-form-item>

                <el-form-item label="承租人：" prop="renter">
                    <el-input v-model="formData.renter"></el-input>
                </el-form-item>

                <el-form-item label="报修日期：" prop="repairDate">
                    <el-date-picker v-model="formData.repairDate" type="date" placeholder="年/月/日">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="报修部位：" prop="repairLocation">
                    <el-input v-model="formData.repairLocation"></el-input>
                </el-form-item>

                <el-form-item label="是否约修：" prop="isBook">
                    <el-select v-model="formData.isBook">
                        <el-option label="是" :value="1" :key="1"></el-option>
                        <el-option label="否" :value="0" :key="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="查勘后报修日期：" prop="repairDateAfterSurvey">
                    <el-date-picker v-model="formData.repairDateAfterSurvey" type="date" placeholder="年/月/日">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="报修项目：" prop="repairItems">
                    <el-input v-model="formData.repairItems"></el-input>
                </el-form-item>

                <el-form-item label="问题说明：" prop="problemDescription">
                    <el-input v-model="formData.problemDescription"></el-input>
                </el-form-item>


                <el-form-item label="问题图片：" prop="name">
                    <el-upload action="http://125.39.77.18:9021/mall-admin/fileUpload/upload" :auto-upload="true"
                        list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove"
                        :file-list="fileList" :on-success="handleUploadSuccess">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="接待人员：" prop="receptionist">
                    <el-input v-model="formData.receptionist"></el-input>
                </el-form-item>

                <el-form-item label="说明：" prop="instructions">
                    <el-input v-model="formData.instructions"></el-input>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="handleSave" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>


        <el-dialog :visible.sync="picdialogVisible" title="查看文件" width="40%">
            <img width="100%" :src="dialogImageUrl" alt="">

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="picdialogVisible = false" size="medium">返 回</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import { listBrokenRepairOrderManage, createBrokenRepairOrderManage, updateBrokenRepairOrderManage, deleteBrokenRepairOrderManage, zhuanGongDan } from '@/api/repair'
import { getArea, getHouseAdmin, getBlockGroup, getBlockByGroup, upload } from '@/api/common'
import { Message } from 'element-ui'
import moment from 'moment';
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 20,
                districtId: "",
                houseAdminId: "",
                blockGroupId: "",
                blockId: "",
                blockAddress: "",
                renter: "",
                repairDate: "",
                isGongdan: ""
            },
            total: 0,

            list: [

            ],

            dialogVisible: false,
            isAdd: 'add',
            formData: {
                districtId: "",
                houseAdminId: "",
                blockGroupId: "",
                blockId: "",
                blockAddress: "",
                renter: "",
                repairDate: "",
                repairLocation: "",
                isBook: null,
                repairDateAfterSurvey: "",
                repairItems: "",
                problemDescription: "",
                receptionist: "",
                instructions: ""
            },

            picdialogVisible: false,
            dialogImageUrl: "",
            fileList: [],
            areaList: [],
            houseAdminList: [],
            houseFormAdminList: [],
            guanpianList: [],
            suoList: [],
            urlList: [],

        };
    },

    computed: {
        getTime() {
            return (val) => {
                if (val) {
                    return moment(val).format("YYYY-MM-DD")
                } else {
                    return ""
                }
            }
        }
    },

    mounted() {
        this.getCommon();
        this.getList();
    },

    methods: {

        getCommon() {
            getArea().then(res => {
                this.areaList = res.data
            })
        },

        handleQuchange(val) {
            this.listQuery.houseAdminId = null;
            if (val) {
                this.getHouseAdmin(val);

            } else {
                this.houseAdminList = [];
            }
        },

        handleFormQuchange(val) {
            this.formData.houseAdminId = null;
            this.handleHouseChange();
            if (val) {
                this.getFormHouseAdmin(val);

            } else {
                this.houseFormAdminList = [];
            }
        },

        getHouseAdmin(val) {
            getHouseAdmin({ DID: val, pageNum: 1, pageSize: 100000 }).then(res => {
                this.houseAdminList = res.data.list;
            })
        },
        getFormHouseAdmin(val) {
            getHouseAdmin({ DID: val, pageNum: 1, pageSize: 100000 }).then(res => {
                this.houseFormAdminList = res.data.list;
            })
        },

        handleHouseChange(val) {
            this.formData.blockGroupId = null
            this.handleGuanpianChange();
            if (val) {
                getBlockGroup({ houseAdminId: val, pageNum: 1, pageSize: 10000 }).then(res => {
                    this.guanpianList = res.data.list;
                })
            } else {
                this.guanpianList = [];
            }
        },

        handleGuanpianChange(val) {

            this.formData.blockId = null
            this.handleSuoChange();
            if (val) {
                getBlockByGroup({ group: val, pageNum: 1, pageSize: 10000 }).then(res => {
                    this.suoList = res.data.list;
                })
            } else {
                this.suoList = [];
            }
        },

        handleSuoChange(val) {

            if (val) {
                this.suoList.forEach(item => {
                    if (item.blockid == val) {
                        this.formData.blockAddress = item.address;

                    }
                })
            } else {
                this.formData.blockAddress = ""
            }
        },
        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        handleRemove(file, fileList) {
            this.fileList = fileList;
            let arr = [...this.urlList];
            let i;

            arr.forEach((item, index) => {
                if (file.response) {
                    if (item.url == file.response.data.url) {
                        i = index
                    }
                } else {
                    if (item.url == file.url) {
                        i = index
                    }
                }
            })

            if (i != undefined) {
                arr.splice(i, 1);
                this.urlList = arr;
            }

        },
        handleUploadSuccess(response, file, fileList) {

            if (response.code == 200) {
                this.fileList = fileList
                this.$message.success('文件上传成功');
                this.urlList.push(response.data);
            } else {
                this.$message.error('文件上传失败');
                let arr = [...fileList.map(i => i)]
                arr.pop();
                this.fileList = arr;
            }
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.picdialogVisible = true;
        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {

            listBrokenRepairOrderManage(this.listQuery).then((response) => {
                this.total = response.data.total;
                this.list = response.data.list;

            });
        },

        resetForm(val) {

            this.$refs[val].resetFields();
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
            this.houseFormAdminList = [];
            this.guanpianList = [];
            this.suoList = [];
            this.fileList = [];

        },

        handleSave() {
            let str = [];
            if (this.urlList.length > 0) {
                this.urlList.forEach(item => {
                    str.push(item.url)
                })
            }


            if (this.isAdd == 'add') {
                createBrokenRepairOrderManage({ ...this.formData, problemImage: str.join(',') }).then(res => {
                    this.handleClose();
                    this.getList();
                    this.$message.success("操作成功")
                })
            }

            if (this.isAdd == 'update') {
                updateBrokenRepairOrderManage({ ...this.formData, problemImage: str.join(',') }).then(res => {
                    this.handleClose();
                    this.getList();
                    this.$message.success("操作成功")
                })
            }
        },



        async handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            let arr = [];
            if (currentData.problemImage != null && currentData.problemImage != "") {
                currentData.problemImage.split(',').forEach(item => {
                    arr.push({
                        url: item,
                        name: item.split('/').slice(-1)[0]
                    })
                })

            }

            await this.handleFormQuchange(currentData.districtId)
            await this.handleHouseChange(currentData.houseAdminId)
            await this.handleGuanpianChange(currentData.blockGroupId)
            this.$nextTick(() => {
                this.fileList = arr;
                this.urlList = arr;
                this.formData = { ...currentData };
            });
        },

        handleBantuo(currentData) {
            this.$confirm(
                "是否转换？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    zhuanGongDan(currentData.id).then(res => {
                        this.getList();
                        this.$message.success("操作成功")
                    })
                })
                .catch(() => { });

        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteBrokenRepairOrderManage(id).then(res => {
                        this.getList();
                        this.$message.success("操作成功")
                    })
                })
                .catch(() => { });
        }
    },
};
</script>

<style scoped>
.el-date-editor.el-input {
    width: 100%;
    cursor: pointer;
}

::v-deep .el-upload-list__item.is-success .el-upload-list__item-status-label {
    display: none !important;
}

::v-deep .el-upload--picture-card {
    width: 80px !important;
    height: 80px !important;
    line-height: 80px !important;
}

::v-deep .el-upload--picture-card:hover,
.el-upload:focus {
    border-color: #008C93;
    color: #008C93;
}

::v-deep .el-upload-list--picture-card .el-upload-list__item {
    width: 80px !important;
    height: 80px !important;
    line-height: 80px !important;
}
</style>